<template>
  <div class="cate">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-row>
        <el-button type="primary" @click="showAddCateAction"
          >添加分类</el-button
        >
      </el-row>
      <!-- 表格 -->
      <TreeTable
        class="table"
        :data="cateAll"
        :columns="columns"
        show-index
        index-text="#"
        :expand-type="false"
        :selection-type="false"
        border
      >
        <template slot="likes" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color: green"
          ></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <template slot="sort" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level == 0" size="mini">一级</el-tag>
          <el-tag
            v-else-if="scope.row.cat_level == 1"
            type="success"
            size="mini"
            >二级</el-tag
          >
          <el-tag v-else type="warning" size="mini">三级</el-tag>
        </template>
        <template slot="handle" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini"
            >编辑</el-button
          >
          <el-button type="danger" icon="el-icon-delete" size="mini"
            >删除</el-button
          >
        </template>
      </TreeTable>

      <!-- 分页 -->
      <el-pagination
        :current-page="cateInfo.pagenum"
        :page-size.sync="cateInfo.pagesize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[3, 5, 10, 15]"
        layout="total,sizes,  prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类弹出框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="dialogFormVisible"
      @click="addCateFormClose"
    >
      <el-form
        :model="addCateForm"
        label-width="100px"
        :rules="rules"
        ref="form"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>

        <el-form-item label="父级分类">
          <el-cascader
            class="cascader"
            v-model="selectedCate"
            :options="ParentCateList"
            :props="cascaderProps"
            @change="ParentCateChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from '@/axios/user'
export default {
  name: 'Cate',
  data() {
    return {
      cateInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      //商品列表的所有数据
      cateAll: [],
      total: 0, //总数据
      columns: [
        {
          label: '分类商品',
          prop: 'cat_name',
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'likes',
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort',
        },
        {
          label: '操作',
          type: 'template',
          template: 'handle',
        },
      ],
      dialogFormVisible: false, //控制弹出框的显示隐藏
      addCateForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0,
      },
      rules: {
        cat_name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
      },
      ParentCateList: [],
      selectedCate: [],
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true,
      },
    }
  },
  methods: {
    async getCateInfo() {
      const { data } = await axios.get('/categories', { params: this.cateInfo })
      //console.log(data)
      if (data.meta.status !== 200) {
        return this.$message.error(data.meta.msg)
      }
      //console.log(data)
      this.cateAll = data.data.result
      this.total = data.data.total
    },
    handleSizeChange(val) {
      //console.log(val)
      this.cateInfo.pagesize = val
      this.getCateInfo()
    },
    handleCurrentChange(val) {
      //console.log(val)
      this.cateInfo.pagenum = val
      this.getCateInfo()
    },
    showAddCateAction() {
      this.getParentCateList()
      this.dialogFormVisible = true
    },
    //添加商品列表数据请求
    async getParentCateList() {
      const { data } = await axios.get('/categories', { params: { type: 2 } })
      console.log(data)
      if (data.meta.status !== 200) {
        return this.$message.error(data.meta.msg)
      }
      this.ParentCateList = data.data
    },
     addCate() {
      this.$refs.form.validate(async (value) => {
        if (value) {
          const { data } = await axios.post('/categories', this.addCateForm)
          console.log(data)
          if(data.meta.status!==201){
              return this.$message.error(data.meta.msg)
          }
          this.$message.success(data.meta.msg)
          this.getCateInfo()
          this.dialogFormVisible = false
        }
      })

      //console.log(this.addCateForm);
    },
    ParentCateChange() {
      console.log(this.selectedCate)
      if (this.selectedCate.length > 0) {
        this.addCateForm.cat_pid =
          this.selectedCate[this.selectedCate.length - 1]
        this.addCateForm.cat_level = this.selectedCate.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    addCateFormClose() {
      this.$refs.form.resetFields()
      this.selectedCate = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },
  },
  created() {
    this.getCateInfo()
  },
}
</script>

<style scoped lang="scss">
.table {
  margin-top: 15px;
}
.cascader {
  width: 100%;
}
</style>